package com.tanhua.management.controller;


import com.tanhua.management.service.LoginService;
import com.tanhua.management.util.NoAuthorization;
import com.tanhua.management.vo.AdminVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("management/system/users")
@Slf4j
public class LoginController {

    @Autowired
    private LoginService loginService;
    /**
     * 这是前端页面传过来的随机的 UUID 值
     * @param uuid
     * @return
     */
    @GetMapping("verification")
    @NoAuthorization
    public ResponseEntity<Void> CreateVerification(@RequestParam(value = "uuid",required = true) String uuid ,
                                                   HttpServletResponse response){
        try {
            boolean boo = this.loginService.CreateVerification(uuid,response);
            if (boo){
                return ResponseEntity.status(200).body(null);
            }
        } catch (Exception e) {
            log.error("生成验证码图片失败",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }


    /**
     * map 中包含前端给的数据 username  password   verificationCode  uuid
     * @param map
     * @return  需要返回一个 token
     */
    @PostMapping("login")
    @NoAuthorization
    public ResponseEntity<Object> login(@RequestBody Map<String ,String > map){
        try {
            //得到前端的数据
            String username = map.get("username");
            String password = map.get("password");
            String code = map.get("verificationCode");
            String uuid = map.get("uuid");
            String token = this.loginService.login(username,password,code,uuid);
            Map<String ,Object> map1 = new HashMap<>();
            map1.put("token",token);
            System.out.println("=================");
            System.out.println(token);
            if (StringUtils.isNotEmpty(token)){
                return ResponseEntity.status(200).body(map1);
            }
        } catch (Exception e) {
            log.error("获取token失败",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }


    /**
     * 用户基本信息
     * @param token
     * @return
     */
    @PostMapping("profile")
    @NoAuthorization
    public ResponseEntity<AdminVo> profile(@RequestHeader("Authorization") String token){
        try {
            String substring = token.replace("Bearer ", "");
            AdminVo adminVo = this.loginService.queryAdminByToken(substring);
            if (adminVo != null){
                return ResponseEntity.status(200).body(adminVo);
            }
        } catch (Exception e) {
            log.error("用户信息查询错误",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    @PostMapping("logout")
    public ResponseEntity<Void> logout(@RequestHeader("Authorization") String token){
        try {
            String substring = token.replace("Bearer ", "");
            boolean boo =  this.loginService.logout(substring);
            if (boo){
                return ResponseEntity.status(200).body(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

}
